const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
async function myfetch(options){
  let obj={
    url:options.url,
    method:options.method || 'GET',
    dataType:options.dataType || 'json'
  }
  if(options.data){
    obj.data=options.data
  }
  let token=wx.getStorageSync('token');
  obj.header={
    'Authorization':token
  }
  if(options.header){
    for(let k in options.header){
      obj.header[k]=options.header[k]
    }
  }
  let ret=await new Promise((resolve,reject)=>{
    obj.success=res=>{
      if(res.statusCode==200){
        resolve({
          ok:true,
          data:res.data,
          header:res.header,
          statusCode:res.statusCode
        })
      }else{
        resolve({
          ok:false,
          data:res.data,
          statusCode:res.statusCode
        })
      }
    }
    obj.fail=err=>{
      resolve({
        ok:false,
        statusCode:0,
        data:(err && err.message)? err.message : 'request failed'
      })
    }
    if(options.isUpload){
      obj.name=options.name;
      obj.filePath=options.filePath;
      wx.uploadFile(obj);
    }else{
      wx.request(obj)
    }
  })
  return ret;
}

let jumpShow=function(e,jumpToPage,option){
  // wx.setStorageSync('videoId', e.target.id)
  // console.log(e.target.id);
  if(jumpToPage=='interact'){
    wx.navigateTo({
      url:`/pages/interact/interact?videoId=${e.target.id}`
    })
  }else if(jumpToPage=='cognize'){
    wx.navigateTo({
      url:`/pages/cognize/cognize?cognize_Classify=${e.target.id}&amount=${option.amount}`
    })
  }else if(jumpToPage=='test'){
    wx.navigateTo({
      url:`/pages/test/test?test_Classify=${e.target.id}&amount=${option.amount}`
    })
  }else if(jumpToPage=='cultureList'){
    wx.navigateTo({
      url:`/pages/cultureList/cultureList?culture_Classify=${e.target.id}&culture_Title=${option.culture_Title}`
    })
  }else if(jumpToPage=='culture'){
    wx.navigateTo({
      url:`/pages/culture/culture?cultureId=${e.target.id}&culture_Classify=${option.culture_Classify}`
    })
  }else{
    wx.navigateTo({
      url:`/pages/studyVideo/studyVideo?videoId=${e.target.id}&studyModule=${option.studyModule}`
    })
  }
}


/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
*/
function formatTimeTwo(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  //number必须是数字类型
  var date = new Date(number);
  
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));
  console.log(returnArr)
  for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
  }
  
  return format;
}


module.exports = {
  formatTime,
  myfetch,
  jumpShow,
  formatTimeTwo,
}
